/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Administracion;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author pato
 */
public class Sucursales extends javax.swing.JInternalFrame {

    /**
     * Creates new form Sucursales
     */
        Connection cn=local_connection.cn;
    PreparedStatement ps;
    ResultSet rs;
    DefaultTableModel dtm;
    FormatoTabla ft;//para el jtable coloread
    public Sucursales() {
        initComponents();
        dtm = (DefaultTableModel) this.jTable1.getModel();
        ft = new FormatoTabla(0, dtm);
        jTable1.setDefaultRenderer(Object.class, ft);
        TableRowSorter<TableModel> elQueOrdena = new TableRowSorter<TableModel>(dtm);
        jTable1.setRowSorter(elQueOrdena);
        cargarDatos();
    }
public  void cargarDatos(){
    dtm.setRowCount(0);
        try {
            
            cn = local_connection.getconnect();
            ps = (PreparedStatement) cn.prepareStatement("select activo,id,nombre,telefono,pass from sucursales");

            rs = ps.executeQuery();
ArrayList<Object[]> array = new ArrayList<Object[]>();            


            //  Object[][] data = new Object[array.size()][jTable1.getColumnCount()];
            //    data = array.toArray();

            while (rs.next()) {

                Object[] columna = new Object[dtm.getColumnCount()];
                for (int i = 0; i < columna.length; i++) {
                    columna[i] = rs.getObject(i + 1);
                }
                array.add(columna);
                //  
            }
            Object[][] objeto = new Object[array.size()][];
            for (int j = 0; j < array.size(); j++) {
                objeto[j] = array.get(j);
            }


            dtm.setDataVector(objeto, new Object[]{"",
                "ID", "Nombre", "Telefono", "Clave"});
            jTable1.removeColumn(jTable1.getColumnModel().getColumn(0));

            
         }catch (SQLException ex) {
            Logger.getLogger(ventasdia.class.getName()).log(Level.SEVERE, null, ex);
        }
}
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        detalle = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        agregar = new javax.swing.JButton();
        baja = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Activo", "ID", "Nombre", "Telefono", "Clave"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTable1);

        detalle.setText("Detalle");
        detalle.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                detalleActionPerformed(evt);
            }
        });

        jButton1.setText("Editar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        agregar.setText("Agregar");
        agregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                agregarActionPerformed(evt);
            }
        });

        baja.setText("Dar de Baja");
        baja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bajaActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(detalle, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(agregar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(baja, javax.swing.GroupLayout.DEFAULT_SIZE, 111, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(detalle)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(agregar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(baja)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(197, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void detalleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_detalleActionPerformed
        if(jTable1.getSelectedRow()!=-1){
            detalleSucursal cuc=new detalleSucursal(-1,(int) dtm.getValueAt(jTable1.convertRowIndexToModel(jTable1.getSelectedRow()), 1),this);
            System.out.println((int) dtm.getValueAt(jTable1.convertRowIndexToModel(jTable1.getSelectedRow()), 1));
            cuc.setVisible(true);
            Principal.estatico.add(cuc);
            cuc.toFront();
            cuc.repaint();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_detalleActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
if(jTable1.getSelectedRow()!=-1){
            detalleSucursal cuc=new detalleSucursal(0,(int) dtm.getValueAt(jTable1.convertRowIndexToModel(jTable1.getSelectedRow()), 1),this);
            cuc.setVisible(true);
            Principal.estatico.add(cuc);
            cuc.toFront();
            cuc.repaint();
        }
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton1ActionPerformed

    private void agregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_agregarActionPerformed

            detalleSucursal cuc=new detalleSucursal(1, -1,this);
            cuc.setVisible(true);
            Principal.estatico.add(cuc);
            cuc.toFront();
            cuc.repaint();
        
        // TODO add your handling code here:
    }//GEN-LAST:event_agregarActionPerformed

    private void bajaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bajaActionPerformed
if(jTable1.getSelectedRow()!=-1){
    try {
        ps=(PreparedStatement)cn.prepareStatement("update sucursales set activo = 0 where id = ?");
        ps.setInt(1, (int) dtm.getValueAt(jTable1.convertRowIndexToModel(jTable1.getSelectedRow()), 1));
        ps.execute();
    } catch (SQLException ex) {
        Logger.getLogger(Sucursales.class.getName()).log(Level.SEVERE, null, ex);
    }
    cargarDatos();
}
        // TODO add your handling code here:
    }//GEN-LAST:event_bajaActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton agregar;
    private javax.swing.JButton baja;
    private javax.swing.JButton detalle;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables
}
